package game.module.nn.processor;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.nn.NnConstants;
import game.module.nn.bean.NnQiangZhuangJiaInfo;
import game.module.nn.bean.NnRound;
import game.module.nn.logic.NnDataManager;
import game.module.user.bean.PlayerBean;
import game.module.user.logic.RoomDataManager;
import lion.common.ArrayUtility;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageNn.C2SNnBackRoom;
import lua.LuaMessageNn.NNMemberInfo;
import lua.LuaMessageNn.S2CNnBackRoom;
import lua.LuaMessageNn.Xiazhu;
import lua.LuaMessageNn.qiangZhuangJia;

@MsgCodeAnn(msgcode = C2SNnBackRoom.id, accessLimit = 1000)
public class NnBackRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(NnBackRoomProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		int playerId = playingRole.getId();
		logger.info("nn back to room,playerId={}", playerId);
		// 房间是否存在
		NnRound ddzRound = NnDataManager.getInstance().getNnRoom(playerId);
		if (ddzRound == null) {
			S2ErrorCode respMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// do
		S2CNnBackRoom respMsg = new S2CNnBackRoom();
		respMsg.room_id = ddzRound.getRoomUuid();
		respMsg.round_count = ddzRound.getRoundSum();
		respMsg.current_round = ddzRound.getRoundCount().shortValue();
		respMsg.members = new NNMemberInfo[ddzRound.getPlayerIds().size()];
		List<Integer> playerIdList = ddzRound.getPlayerIds();
		int i = 0;
		for (Integer aPlayerId : playerIdList) {
			PlayerBean poc = RoomDataManager.getInstance().getPlayerBean(aPlayerId);
			respMsg.members[i++] = new NNMemberInfo(aPlayerId, poc.getHeadimg(), 1, poc.getSex().shortValue(),
					poc.getName());
		}
		int mjIndex = playerIdList.indexOf(playerId);
		respMsg.my_cards = null;
		if (ddzRound.getCards() != null)
			respMsg.my_cards = ArrayUtility.toByteArray(ddzRound.getCards().get(mjIndex));
		List<NnQiangZhuangJiaInfo> list = ddzRound.getQiangZhuangJiaInfoList();
		if (ddzRound.getQiangZhuangJiaInfoList()!=null && ddzRound.getQiangZhuangJiaInfoList().size() > 0) {
			respMsg.qiangZhuangJiaList = new qiangZhuangJia[ddzRound.getQiangZhuangJiaInfoList().size()];
			int j = 0;
			for (NnQiangZhuangJiaInfo aplayerinfo : list) {
				int playerindex = aplayerinfo.getPlayerIndex();
				respMsg.qiangZhuangJiaList[j] = new qiangZhuangJia();
				respMsg.qiangZhuangJiaList[j].current_qiang_index = (byte) playerindex;
				respMsg.qiangZhuangJiaList[j].current_qiang_score = aplayerinfo.getCallRate();
				j++;

			}
		}
		respMsg.zhuangjia_index = -1;
		if (ddzRound.getZhuangJiaIndex() != null) {
			int zhuangjia = ddzRound.getZhuangJiaIndex();
			respMsg.zhuangjia_index = (byte) zhuangjia;
		}
		respMsg.zhungjia_rate = -1;
		if (ddzRound.getZhuangJiaRate() != null) {
			short zhuangjia_rate = ddzRound.getZhuangJiaRate();
			respMsg.zhungjia_rate = (byte) zhuangjia_rate;
		}
		respMsg.xianjiaxiazhu = null;
		if (ddzRound.getXianJiaXiaZhu()!=null && ddzRound.getXianJiaXiaZhu().size() > 0) {
			respMsg.xianjiaxiazhu = new Xiazhu[ddzRound.getXianJiaXiaZhu().size()];
			Map<Integer, Short> xiazhu = ddzRound.getXianJiaXiaZhu();
			int m = 0;
			for (Map.Entry<Integer, Short> entry : xiazhu.entrySet()) {
				int playindex = entry.getKey();
				respMsg.xianjiaxiazhu[m] = new Xiazhu();
				respMsg.xianjiaxiazhu[m].xiazhu_index = (byte) playindex;
				respMsg.xianjiaxiazhu[m].xiazhu_score = entry.getValue();
				m++;
			}
		}
		if (ddzRound.getScore() != null)
			respMsg.score = ddzRound.getScore();

		Map<Integer, byte[]> peiPaiFinishPlayers = ddzRound.getPeiPaiFinish();
		if (peiPaiFinishPlayers!=null && !peiPaiFinishPlayers.isEmpty()) {
			int[] peipai_play = new int[ddzRound.getPeiPaiFinish().size()];
			int peipai_index = 0;
			for (Map.Entry<Integer, byte[]> entry : peiPaiFinishPlayers.entrySet()) {
			//	peipai_play[]
				peipai_play[peipai_index] = entry.getKey();
				peipai_index++;
			}
			respMsg.peipai_finishPlayer = peipai_play;
		} else {
			respMsg.peipai_finishPlayer = null;
		}
		if(ddzRound.getStartVoteTime()>0) {
			respMsg.votedissPlayer = ddzRound.getVoteplayerIndex();
			byte vote[] = new byte[ddzRound.getPlayerIds().size()];
			boolean dissvote[] = ddzRound.getVotediss();
			int k = 0 ;
			for(boolean aVote:dissvote) {
				if(aVote)
					vote[k]=1;
				k++;
			}
			respMsg.votedissstate = vote;
			respMsg.votedisstime = (int)(NnConstants.AUTODISSMISSROOMTIME-((System.currentTimeMillis()-ddzRound.getStartVoteTime())/1000));
		}
		respMsg.is_jindou = ddzRound.getIs_jindou();
		respMsg.jindouDi = ddzRound.getJindou();
		respMsg.create_time = (int) (ddzRound.getCreate_time() / 1000);
		logger.info("peipaiwancheng = {}",respMsg.peipai_finishPlayer);
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {

	}

}
